<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="typeName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="listTypeTree"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="rowguid"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
          <!-- <el-form-item label="性能类型" prop="performancetypeguid">
            <el-select v-model="queryParams.performancetypeguid"  placeholder="请选择性能类型" clearable>
              <el-option
                v-for="m in typeList"
                :key="m.rowguid"
                :label="m.cname"
                :value="m.rowguid"
              />
            </el-select>
          </el-form-item> -->
          <el-form-item label="参数中文名" prop="cname">
            <el-input
              v-model="queryParams.cname"
              placeholder="请输入参数中文名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="参数英文名" prop="ename">
            <el-input
              v-model="queryParams.ename"
              placeholder="请输入参数英文名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:defined:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:defined:edit']"
            >修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:defined:remove']"
            >删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              :disabled="disableTable"
              @click="createTable"
            >建表</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="definedList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column label="性能类型ID" align="center" prop="performancetypeguid" /> -->
          <el-table-column label="性能类型" align="center" prop="performancetypename" />
          <el-table-column label="参数中文名" align="center" prop="cname" />
          <el-table-column label="参数英文名" align="center" prop="ename" />
          <el-table-column label="数据类型(代码集 浮点 字符串)" align="center" prop="dateType" />
          <el-table-column label="数据长度" align="center" prop="dataLen" />
          <el-table-column label="数据精度" align="center" prop="decimalPoint" />
          <el-table-column label="是否可以为空" align="center" prop="isNull" />
          <el-table-column label="是否参与计算" align="center" prop="isComputer" />
          <el-table-column label="排序号" align="center" prop="showorder" />
          <el-table-column label="操作时间" align="center" prop="opTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.opTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:defined:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:defined:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="性能类型" prop="performancetypeguid">
          <el-select v-model="form.performancetypeguid"  placeholder="请选择性能类型" clearable>
            <el-option
              v-for="m in typeList"
              :key="m.rowguid"
              :label="m.cname"
              :value="m.rowguid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="参数中文名" prop="cname">
          <el-input v-model="form.cname" placeholder="请输入参数中文名" />
        </el-form-item>
        <el-form-item label="参数英文名" prop="ename">
          <el-input v-model="form.ename" placeholder="请输入参数英文名" />
        </el-form-item>
        <el-form-item label="数据类型(代码集 浮点 字符串)" prop="ename">
          <el-select v-model="form.dateType"  placeholder="数据类型(代码集 浮点 字符串)" clearable>
            <el-option
              v-for="m in dateType"
              :key="m.value"
              :label="m.name"
              :value="m.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="数据长度" prop="dataLen">
          <el-input v-model="form.dataLen" placeholder="请输入数据长度" />
        </el-form-item>
        <el-form-item label="数据精度" prop="decimalPoint">
          <el-input v-model="form.decimalPoint" placeholder="请输入数据精度" />
        </el-form-item>
        <el-form-item label="是否可以为空" prop="isNull">
          <el-select v-model="form.isNull"  placeholder="请输入是否可以为空" clearable>
            <el-option
              v-for="m in isNull"
              :key="m.value"
              :label="m.name"
              :value="m.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="是否参与计算" prop="isComputer">
          <el-select v-model="form.isComputer"  placeholder="请输入是否参与计算" clearable>
            <el-option
              v-for="m in isComputer"
              :key="m.value"
              :label="m.name"
              :value="m.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="排序号" prop="showorder">
          <el-input v-model="form.showorder" placeholder="请输入排序号" />
        </el-form-item>
<!--        <el-form-item label="操作时间" prop="opTime">
          <el-date-picker clearable
            v-model="form.opTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择操作时间">
          </el-date-picker>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDefined, getDefined, delDefined, addDefined, updateDefined,createTable } from "@/api/business/paramdefined";
import { listType } from "@/api/business/performancetype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import defaultSettings from '@/config/defaultSettings';
export default {
  name: "Defined",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      definedList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        performancetypeguid: null,
        cname: null,
        ename: null,
        dateType: null,
        dataLen: null,
        decimalPoint: null,
        isNull: null,
        showorder: null,
        opTime: null
      },
      // 查询参数
      mParams: {
        pageNum: 1,
        pageSize: 10000,
        cname: null,
        ename: null,
        description: null,
        excelTempalte: null,
        opTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cname: [
          { required: true, message: "参数中文名不能为空", trigger: "blur" }
        ],
        ename: [
          { required: true, message: "参数英文名不能为空", trigger: "blur" }
        ],
        dateType: [
          { required: true, message: "数据类型(代码集 浮点 字符串)不能为空", trigger: "change" }
        ],
      },
      // isNull:[
      //   {value:"0",name:"是"},
      //   {value:"1",name:"否"}
      // ],
      // dateType:[
      //   {value:"0",name:"代码集"},
      //   {value:"1",name:"浮点"},
      //   {value:"2",name:"字符串"}
      // ],
      typeList:[],
      defaultProps: {
        children: "children",
        label: "label"
      },
      listTypeTree:[
        {children:[],id:100,label:"性能类型"}
      ],
      typeName: undefined,
      dateType: defaultSettings.dateType,
      isNull: defaultSettings.isNull,
      isComputer: defaultSettings.isComputer,
      disableTable : true
    };
  },
  created() {
    this.getList();
    this.getPerList();
  },
  watch: {
    // 根据名称筛选部门树
    typeName(val) {
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listDefined(this.queryParams).then(response => {
        this.definedList = response.rows;
        this.total = response.total;
        if(response.rows ==null || response.rows.length==0){
          this.disableTable = true;
        }
        this.loading = false;
      });
    },
    getPerList(){
      listType(this.mParams).then(response => {
        this.typeList = response.rows;
        response.rows.forEach(d => {
          let child = {id:d.rowguid,label:d.cname}
          this.listTypeTree[0].children.push(child)
        })
        this.$nextTick(() => {
            // document.querySelector('.el-tree-node__content').click()
            //默认选中第一层级的第一个节点
            document.querySelector('.el-tree-node__children .el-tree-node__content').click();
        })
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rowguid: null,
        performancetypeguid: this.form.performancetypeguid,
        cname: null,
        ename: null,
        dateType: null,
        dataLen: null,
        decimalPoint: null,
        isNull: null,
        showorder: null,
        opTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rowguid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加测试参数定义";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rowguid = row.rowguid || this.ids
      getDefined(rowguid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测试参数定义";
      });
    },
    /** 建表操作 */
    createTable(guid) {
      createTable(this.queryParams.performancetypeguid).then(() => {
        this.$modal.msgSuccess("建表成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rowguid != null) {
            updateDefined(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDefined(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rowguids = row.rowguid || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + rowguids + '"的数据项？').then(function() {
        return delDefined(rowguids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/defined/export', {
        ...this.queryParams
      }, `defined_${new Date().getTime()}.xlsx`)
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      if(data.id==100){
        this.queryParams.performancetypeguid = null;
        this.disableTable = true;
      }else{
        this.queryParams.performancetypeguid = data.id;
        this.form.performancetypeguid = data.id;
        this.disableTable = false;
      }
      this.handleQuery();
    },
  }
};
</script>
